package org.example.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.argument.LoginUserArgumentResolver;
import org.example.filter.InternalApiHandlerInterceptor;
import org.example.filter.TokenHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.CacheControl;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final InternalApiHandlerInterceptor internalApiHandlerInterceptor;
    private final TokenHandlerInterceptor tokenHandlerInterceptor;

    private final LoginUserArgumentResolver loginUserArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("注册 InternalApiHandlerInterceptor");
        //排除静态资源
        registry.addInterceptor(internalApiHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**", "/webjars/**",
                        "/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs", "/csrf",
                        "/doc.html", "/swagger-ui/**", "/swagger/**"
                );
        registry.addInterceptor(tokenHandlerInterceptor)
                .addPathPatterns("/**")  .excludePathPatterns("/static/**", "/webjars/**",
                        "/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs", "/csrf",
                        "/doc.html", "/swagger-ui/**", "/swagger/**"
                );;


    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        // 添加自定义参数解析器
        resolvers.add(loginUserArgumentResolver);

        // 可以添加多个解析器，并控制顺序
        // resolvers.add(0, anotherResolver); // 设置为最高优先级
    }

    @Override
    public void addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry registry) {


        // 或者覆盖默认的静态资源路径
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }


    @Bean
    public ViewResolver htmlViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/static/htmls/");
        resolver.setSuffix(".html");
        resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
        resolver.setViewClass(InternalResourceView.class);
        return resolver;
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 对所有路径生效
                .allowedOriginPatterns("http://localhost:*") // 允许的域，根据实际情况修改
                .allowedMethods("*") // 允许的方法
                .allowedHeaders("*") // 允许的头信息
                .allowCredentials(true) // 是否发送Cookie
                .maxAge(3600); // 预检请求的有效期（秒）
    }


}